import MTExternalContent
from pymt import *
import os
current_dir = os.path.dirname(__file__)

class MTText(MTExternalContent.MTExternalContent):
    name = 'text'
    def __init__(self, listPages,kinetics, filename, **kwargs):
        # kwargs.setdefault('do_rotation', False)
        kwargs.setdefault('cls', 'text')
        kwargs.setdefault('type', 'text')		
        super(MTText, self).__init__(**kwargs)
        self.listPages=listPages		
        self.kinetics=kinetics
        self.hold=False
        self.in_move_inside=False
        self.in_zone=False	
        self.in_move_outside=False
        self.zone=None
        self.label= ""
        self.filename=filename
        self.load_file(self.filename)		

	
    def on_touch_move(self, touch):	
        if not self.collide_point(*touch.pos):
			return False	
        if(touch.is_held and (touch.xmot == 0 and touch.ymot == 0)):
			self.hold=True
			self.center=(touch.x,touch.y)			
        if(self.hold==True):
			for page in self.listPages:
				self.page=page			
				media_zones=page._get_text_zones()
				for zone in media_zones:
					pos_zone=self.get_position_zone(zone,page)
					if(self.in_zone==False and (self.center[0]> pos_zone[0] and self.center[0]< pos_zone[0]+zone.width\
					and self.center[1]> pos_zone[1] and self.center[1]< pos_zone[1]+zone.height)):
						print "go inside"
						self.in_move_inside=True
						self.zone=zone
						return MTExternalContent.MTExternalContent.on_touch_move(self, touch)						
					if(self.in_zone==True):
						print "go outside"
						self.kinetics.add_widget(self)
						zone.remove_widget(self)						
						self.pos=(pos_zone[0]-(self.width/2),pos_zone[1]+(self.height/2))
						self.in_move_outside=True
						return False
					else:
						print "move"					
						self.in_move_inside=False	
						self.in_move_outside=False
			return MTExternalContent.MTExternalContent.on_touch_move(self, touch)
	
	
    def on_touch_up(self, touch):
		if (self.in_move_inside==True):
			print "inside"
			self.in_zone=True
			self.in_move_inside=False
			self.size=self.zone.size
			self.pos=(self.zone.width/2,self.zone.height/2)
			rotation = 360 - self.rotation
			MTExternalContent.MTExternalContent.apply_angle_scale_trans(self, rotation, 1., Vector(0, 0), Vector(*self.pos))				
			if (self.zone._get_content()!=None):
				pos_zone=self.get_position_zone(self.zone,self.page)
				self.kinetics.add_widget(self.zone._get_content())
				self.zone._get_content().pos=(pos_zone[0]-(self.width/2),pos_zone[1]+(self.height/2))
				self.zone._get_content().in_zone=False
				self.zone._get_content().in_move_outside=False
				self.zone.remove_widget(self.zone._get_content())				
			self.zone.add_widget(self)			
			self.kinetics.remove_widget(self)
		if (self.in_move_outside ==True):
			print "outside"
			self.in_zone=False
			self.in_move_outside=False

		self.hold=False
		return MTExternalContent.MTExternalContent.on_touch_up(self, touch)
	
    def get_position_zone(self, zone, page):
		x=(zone.x-(zone.width/2))+(page.x-(page.width/2))
		y=(zone.y-(zone.height/2))+(page.y-(page.height/2))
		pos=(x,y)
		return pos

    def load_file(self, filename):
		f = open(current_dir+filename, 'r')
		lignes=f.readlines()
		
		for i in lignes:
			self.label+=i
		
    def draw(self):
		MTExternalContent.MTExternalContent.draw(self)
		w,h=drawLabel(label=self.label,pos=(0,0),center=False, color=(0,0,0,255),font_size=10, size=self.size)
		if (h<self.height or h>self.height):
			self.height=h
		if (w<self.width or w>self.width):
			self.width=w		

    def _get_label(self):
        return self.label
    id = property(_get_label)